import {
    login,
    getUserInfo
} from '@/api/user'
import {
    setToken,
    getToken,
    setTagNavListInLocalstorage
} from '@/libs/util'

export default {
    state: {
        userName: '',
        userId: '',
        userType: -1,
        avatarImgPath: '',
        token: getToken(),
        access: '',
        hasGetInfo: false,
        permissions: {},
        profile: {}
    },
    mutations: {
        /**
         *
         * @param {*} state
         * @param {*} avatarPath
         */
        setAvatar(state, avatarPath) {
            state.avatarImgPath = avatarPath
        },

        /**
         *
         * @param {*} state
         * @param {*} id
         */
        setUserId(state, id) {
            state.userId = id
        },

        /**
         *
         * @param {*} state
         * @param {*} name
         */
        setUserName(state, name) {
            state.userName = name
        },

        /**
         *
         * @param {*} state
         * @param {*} access
         */
        setAccess(state, access) {
            state.access = access
        },

        /**
         *
         * @param {*} state
         * @param {*} token
         */
        setToken(state, token) {
            state.token = token
            setToken(token)
        },

        /**
         *
         * @param {*} state
         * @param {*} status
         */
        setHasGetInfo(state, status) {
            state.hasGetInfo = status
        }
    },
    getters: {},
    actions: {
        /**
         * 验证码登录
         * @param {*} param0
         * @param {*} param1
         */
        handleLogin({
            commit
        }, {
            phone,
            captcha
        }) {
            phone = phone.trim()
            return new Promise((resolve, reject) => {
                login({
                    phone,
                    captcha
                }).then(res => {
                    const data = res.data
                    if (data.code === 200 && data && data.data) {
                        commit('setToken', data.data)
                    } else {

                    }
                    resolve(res)
                }).catch(err => {
                    commit('setToken', 'data.data')
                    reject(err)
                })
            })
        },

        /**
         * 退出登录
         * @param {*} param0
         */
        handleLogOut({
            state,
            commit
        }) {
            return new Promise((resolve, reject) => {
                commit('setToken', '')
                commit('setAccess', [])
                commit('setPermissions', {})
                setTagNavListInLocalstorage([]);
                resolve()
            })
        },

        /**
         * 获取用户相关信息
         * @param {*} param0
         */
        getUserInfo({ state, commit }) {
            return new Promise((resolve, reject) => {
                try {
                    getUserInfo(state.token).then(res => {
                        const data = res.data
                        commit('setAvatar', data.avatar)
                        commit('setUserName', data.name)
                        commit('setUserId', data.user_id)
                        commit('setAccess', data.access)
                        commit('setPermissions', data.permissions)
                        commit("setUserType", data.user_type);
                        commit('setHasGetInfo', true)
                        resolve(data)
                    }).catch(err => {
                        reject(err)
                    })
                } catch (error) {
                    reject(error)
                }
            })
        }
    }
}